package com.example.houseman.util;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyuncs.exceptions.ClientException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author 王国庆
 */
@Component
@Slf4j
@RefreshScope
public class FileOSSUtil {

    // 阿里云API的内或外网域名

    @Value("${aliyun.oss.endpoint}")
    private static String endpoint;

    // 阿里云API的密钥Access Key ID

    @Value("${aliyun.oss.accessKeyId}")
    private static String accessKeyId;

    // 阿里云API的密钥Access Key Secret

    @Value("${aliyun.oss.accessKeySecret}")
    private static String accessKeySecret;

    // 阿里云API的bucket名称

    @Value("${aliyun.oss.bucketName}")
    private static String bucketName;

    // 赋值
    public static void settingIniOSS(String endpoint, String accessKeyId, String accessKeySecret, String bucketName) {
        FileOSSUtil.endpoint = endpoint;
        FileOSSUtil.accessKeyId = accessKeyId;
        FileOSSUtil.accessKeySecret = accessKeySecret;
        FileOSSUtil.bucketName = bucketName;
    }

    public static String uploadFile(String objectKey, MultipartFile multipartFile) throws OSSException, ClientException, FileNotFoundException {
        log.info("-----------上传文件为-------------" + multipartFile.getOriginalFilename());
        //settingIniOSS(endpoint,accessKeyId,accessKeySecret,bucketName);
        // 创建OSSClient的实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        //objectMetadata.setContentType(fileType);



        //OSS 对象的元数据。 它有用户自定义的元数据，以及一些发送到OSS的标准http头，如Content-Length、ETag等。
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType("image/jpg"); //设置文件内容类型标头，解决不能预览
        try {
            if (multipartFile.getSize() != 0 && !"".equals(multipartFile.getName())) {
                // getInputStream()返回一个InputStream以从中读取文件的内容。通过此方法就可以获取到流
                InputStream multipartFileInputStream = multipartFile.getInputStream();
                PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectKey, multipartFileInputStream, objectMetadata);
                ossClient.putObject(putObjectRequest);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            ossClient.shutdown();
            return "http://" + bucketName + "." + endpoint + "/" + objectKey;
//            return "http://" + bucketName + "." + endpoint + "/" + multipartFile.getOriginalFilename();
        }

    }
}
